package com.roncoo.eshop.inventory.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by hp on 2018/9/29.
 */
@Configuration
public class JedisClusterConfig {

    @Autowired
    private RedisProperties redisProperties;


    @Bean
    public JedisCluster getJedisCluster() {
        String[] serverArray = redisProperties.getClusterNodes().split(",");
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();

        for(String server : serverArray ){
            String[] hostAndPortArray = server.split(":");
            HostAndPort hostAndPort = new HostAndPort(hostAndPortArray[0], Integer.valueOf(hostAndPortArray[1]));
            nodes.add(hostAndPort);
        }

        return new JedisCluster(nodes,redisProperties.getCommandTimeout());
    }
}
